package com.atguigu.tingshu.account.api;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.annotation.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.vo.account.AccountLockVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Tag(name = "用户账户管理")
@RestController
@RequestMapping("api/account/userAccount")
@SuppressWarnings({"all"})
public class UserAccountApiController {

	@Autowired
	private UserAccountService userAccountService;

	//远程调用接口：账户初始化
	@Operation(summary = "初始化账户")
	@GetMapping("/initUserAccount/{userId}")
	public Result initUserAccount(@PathVariable(value = "userId") Long userId){
		userAccountService.initUserAccount(userId);
		return Result.ok();
	}

	//获取账户可用余额
	@GuiGuLogin
	@Operation(summary = "获取账户可用余额")
	@GetMapping("/getAvailableAmount")
	public Result<BigDecimal> getAvailableAmount(){
		Long userId = AuthContextHolder.getUserId();
	BigDecimal availableBalance =	userAccountService.getAvailableAmount(userId);
	return Result.ok(availableBalance);
	}

	//检查并扣减余额
	@Operation(summary = "检查并扣减余额")
	@PostMapping("/checkAndDeduct")
	public Result CheckAndDeduct(@RequestBody AccountLockVo accountLockVo){
	userAccountService.CheckAndDeduct(accountLockVo);
		return Result.ok();
	}


}

